﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using EasyGo.Domain.Model;
using EasyGo.Services;

namespace EasyGo.Web.Controllers
{
    public class CompanyController : ApiController
    {
        private readonly ICompanyService company;

        public CompanyController(ICompanyService company)
        {
            this.company = company;
        }

        public IEnumerable<Company> Get()
        {
            return this.company.GetAllCompanies();
        }

        public HttpResponseMessage Get(int id)
        {
            Company one = company.GetById(id);
            if (one == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Company not found");
            }

            return Request.CreateResponse(HttpStatusCode.OK, one);
        }

        public HttpResponseMessage Post(Company value)
        {
            if (!ModelState.IsValid)
            {
                return new HttpResponseMessage(HttpStatusCode.BadRequest);
            }
            Company inserted = company.CreateCompany(value);
            var response = Request.CreateResponse(HttpStatusCode.Created, inserted);

            string uri = Url.Link("DefaultApi", new { id = inserted.Id });
            response.Headers.Location = new Uri(uri);
            return response;
        }

        // PUT api/company/5
        public void Put(Company value)
        {
            this.company.UpdateCompany(value);
        }

        // DELETE api/company/5
        public HttpResponseMessage Delete(int id)
        {
            company.Delete(id);
            return new HttpResponseMessage(HttpStatusCode.Accepted);
        }
    }
}
